package org.origin.ferret.system.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.origin.centre.model.enums.XStatus;
import org.origin.centre.model.enums.XUsable;
import org.origin.centre.utils.UserUtil;
import org.origin.ferret.system.mapper.SysGrantMapper;
import org.origin.ferret.system.mapper.SysMenuMapper;
import org.origin.ferret.system.mapstruct.SysMenuMapstruct;
import org.origin.ferret.system.model.vo.menu.SysMenuVo;
import org.origin.ferret.system.service.SysGrantService;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.origin.ferret.system.model.entity.table.SysMenuEntityTableDef.SYS_MENU_ENTITY;

/**
 * 系统授权-服务
 *
 * @author ferret
 * @version 2024-05-08
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysGrantServiceImpl implements SysGrantService {
    private final SysGrantMapper grantMapper;
    private final SysMenuMapper menuMapper;
    private final SysMenuMapstruct menuMapstruct;

    @Override
    public List<SysMenuVo> getMenusByUser(String userId, boolean isAdmin) {
        if(isAdmin){
            return this.menuMapstruct.toVos(this.menuMapper.selectListByQuery(QueryWrapper.create()
                    .where(SYS_MENU_ENTITY.STATUS.eq(XUsable.USABLE.getCode()))
                    .orderBy(SYS_MENU_ENTITY.SORT.asc())
            ));
        }
        return this.menuMapstruct.toVos(this.grantMapper.selectMenusByUser(userId));
    }

}
